﻿using System;
using System.Collections.Generic;

public static class GlobalMembersLCUI_Queue
{
	// ***************************************************************************
	// * LCUI_Queue.h -- basic queue processing
	// * 
	// * Copyright (C) 2012-2013 by
	// * Liu Chao
	// * 
	// * This file is part of the LCUI project, and may only be used, modified, and
	// * distributed under the terms of the GPLv2.
	// * 
	// * (GPLv2 is abbreviation of GNU General Public License Version 2)
	// * 
	// * By continuing to use, modify, or distribute this file you indicate that you
	// * have read the license and understand and accept it fully.
	// *  
	// * The LCUI project is distributed in the hope that it will be useful, but 
	// * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
	// * or FITNESS FOR A PARTICULAR PURPOSE. See the GPL v2 for more details.
	// * 
	// * You should have received a copy of the GPLv2 along with this file. It is 
	// * usually in the LICENSE.TXT file, If not, see <http://www.gnu.org/licenses/>.
	// * ***************************************************************************

	// ****************************************************************************
	// * LCUI_Queue.h -- 基本的队列处理
	// *
	// * 版权所有 (C) 2012-2013 归属于
	// * 刘超
	// * 
	// * 这个文件是LCUI项目的一部分，并且只可以根据GPLv2许可协议来使用、更改和发布。
	// *
	// * (GPLv2 是 GNU通用公共许可证第二版 的英文缩写)
	// * 
	// * 继续使用、修改或发布本文件，表明您已经阅读并完全理解和接受这个许可协议。
	// * 
	// * LCUI 项目是基于使用目的而加以散布的，但不负任何担保责任，甚至没有适销性或特
	// * 定用途的隐含担保，详情请参照GPLv2许可协议。
	// *
	// * 您应已收到附随于本文件的GPLv2许可协议的副本，它通常在LICENSE.TXT文件中，如果
	// * 没有，请查看：<http://www.gnu.org/licenses/>. 
	// * ***************************************************************************
	//#if ! __LCUI_QUEUE_H__
	//#define __LCUI_QUEUE_H__

	////******** 队列的使用模式 ******
	//#define QUEUE_MODE_FREE
	//#define QUEUE_MODE_READ
	////C++ TO C# CONVERTER NOTE: The following #define macro was replaced in-line:
	////ORIGINAL LINE: #define QUEUE_MODE_WRITE 1<<1
	//#define QUEUE_MODE_WRITE
	////C++ TO C# CONVERTER NOTE: The following #define macro was replaced in-line:
	////ORIGINAL LINE: #define QUEUE_MODE_LOCK 1<<2
	//#define QUEUE_MODE_LOCK
	////***************************

	//#define MAX_TID

	public static  LCUI_Queue LCUI_Queue = new LCUI_Queue();
	//********************************************************************

	// 为队列设定互斥锁，使之只能被一个线程使用 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public int Queue_Lock(ref LCUI_Queue queue);

	// 解开队列的互斥锁 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public int Queue_Unlock(ref LCUI_Queue queue);
private delegate void funcDelegate(IntPtr NamelessParameter);

	// 初始化队列 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public void Queue_Init(ref LCUI_Queue queue, size_t element_size, funcDelegate func);

	// 
	// * 功能：设定队列成员类型为指针 
	// * 说明：如果队列只是用于存放指针，并且不希望队列销毁后，指针指向的内存空间也被释放，可
	// * 使用该函数设置。
	// * 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public void Queue_UsingPointer(ref LCUI_Queue queue);

	// 获取队列当前总成员数量 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public int Queue_GetTotal(ref LCUI_Queue queue);

	// 
	// * 功能：设定队列使用的数据储存模式
	// * 说明：只能在初始化后且未加入成员时使用该函数
	// * 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public int Queue_SetDataMode(ref LCUI_Queue queue, _Queue_DataMode mode);

	// 交换队列中指定位置两个成员的位置 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public int Queue_Swap(ref LCUI_Queue queue, int pos_a, int pos_b);

	// 释放队列占用的内存资源 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public void Queue_Destroy(ref LCUI_Queue queue);

	// 
	// * 功能：从队列中获取指定位置的成员 
	// * 说明：成功返回指向该成员的指针，失败返回NULL
	// * 注意：请勿对返回的指针进行free操作
	// * 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public IntPtr Queue_Get(ref LCUI_Queue queue, int pos);

	//* 向队列中指定位置插入成员 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public IntPtr Queue_Insert(ref LCUI_Queue queue, int pos, IntPtr data);

	//* 向队列中指定位置插入成员的指针 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public IntPtr Queue_InsertPointer(ref LCUI_Queue queue, int pos, IntPtr data);

	// 将队列中指定位置的成员移动至目的位置 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public int Queue_Move(ref LCUI_Queue queue, int des_pos, int src_pos);

	// 覆盖队列中指定位置的成员内存空间里的数据 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public int Queue_Replace(ref LCUI_Queue queue, int pos, IntPtr data);

	// 覆盖队列中指定位置的成员指针 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public int Queue_ReplacePointer(ref LCUI_Queue queue, int pos, IntPtr data);

	// 打印队列信息，一般用于调试 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public void Queue_PrintInfo(ref LCUI_Queue queue);

	// 
	// * 功能：将新的成员添加至队列 
	// * 说明：这个函数只是单纯的添加成员，如果想有更多的功能，需要自己实现
	// * 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public IntPtr Queue_Add(ref LCUI_Queue queue, IntPtr data);

	// 
	// * 功能：将新的成员添加至队列 
	// * 说明：与Queue_Add函数不同，该函数只是修改指定位置的成员指针指向的地址，主要用
	// * 与部件队列的处理上，有的部件需要从一个队列转移到另一个队列上，不重新分配内存空间，
	// * 直接使用原来的内存地址，这是为了避免部件转移所在队列后，部件指针无效的问题。
	// * 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public IntPtr Queue_AddPointer(ref LCUI_Queue queue, IntPtr data);

	// 将一个队列拼接至另一个队列的末尾 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public int Queue_Cat(ref LCUI_Queue des, ref LCUI_Queue src);

	// 检测队列是否为空 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public LCUI_BOOL Queue_Empty(ref LCUI_Queue queue);

	// 查找指定成员指针所在队列中的位置 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public int Queue_Find(ref LCUI_Queue queue, IntPtr p);

	// 从队列中删除一个成员，并释放该成员占用的内存资源 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public int Queue_Delete(ref LCUI_Queue queue, int pos);

	// 从队列中删除一个成员指针，不对该指针指向的内存进行释放 
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
	//public int Queue_DeletePointer(ref LCUI_Queue queue, int pos);
//C++ TO C# CONVERTER TODO TASK: The following statement was not recognized, possibly due to an unrecognized macro:
	//LCUI_END_HEADER

	////#endif
}

public enum _Queue_DataMode: int
{
	QUEUE_DATA_MODE_ARRAY = 0,
	QUEUE_DATA_MODE_LINKED_LIST = 1
}

//************** 链表的结点 *****************
public class LCUI_Node
{
	public IntPtr data;
	public LCUI_Node next;
	public LCUI_Node prev;
}
//*****************************************

//***************************** 队列 **********************************
public class LCUI_Queue
{
	//public LCUI_Mutex mutex = new LCUI_Mutex(); //*< 队列互斥锁 
//C++ TO C# CONVERTER TODO TASK: C# does not allow bit fields:
	public int member_type=2; //*< 成员类型 
//C++ TO C# CONVERTER TODO TASK: C# does not allow bit fields:
	public int data_mode=2; //*< 数据储存方式（数组/链表） 
	public Action<IntPtr> calls;
	public IntPtr data_array; //*< 记录队列成员(数组模式) 
	public LCUI_Node data_head_node = new LCUI_Node(); //*< 记录队列成员(链表模式) 

	//public size_t element_size = new size_t(); //*< 成员的占用的内存空间大小，单位为字节 
	public int total_num; //*< 记录队列成员数量 
	public int max_num; //*< 最大的数量 
	public delegate void destroy_funcDelegate(IntPtr NamelessParameter); //*< 析构函数，当销毁队列成员时，会调用它进行销毁 
	public destroy_funcDelegate destroy_func;

	public List<object> list =new List<object> () ;
	public List<LCUI_Widget_> widgetList=new List<LCUI_Widget_>();
	public Type type;
}

public class My_Queue:My_Widget
{
	public static int Queue_GetTotal(LCUI_Queue library)
    {
		try
		{
			return library.list.Count;
		}
		catch
        {
			return 0;
        }
	}

	public static object Queue_Get(LCUI_Queue library,int index)
	{
		if (library.list.Count < index + 1)
			return null;
		return library.list [index];
	}

	public static object Queue_Add(LCUI_Queue library, object data)
	{
	    library.list.Add (data);
		return data;
	}

	public static bool Queue_Add(LCUI_Queue library, object data,bool isTrue=true)
	{
		library.list.Add(data);
		return true;
	}

	public static void Queue_Move(LCUI_Queue library, int index,int endIndex)
    {
		//object a = library.list[index];
		//library.list[index] = library.list[endIndex];
		//library.list[endIndex] = a;

	}

	public static void Queue_DeletePointer(LCUI_Queue library, int index)
    {
		library.list.Remove(library.list[index]);
	}

	public static object Queue_Insert(LCUI_Queue library,int index, object data)
	{
		library.list.Insert(index, data);
		return data;
	}

	public static bool Queue_Delete(LCUI_Queue library, int index)
	{
		if(library ==null)
		    return false;
		else if(library.list == null|| library.list.Count <=0)
			return false;
		return library.list.Remove(library.list[index]);
	}

    public static void Queue_Destroy(LCUI_Queue library)
	{
	   library.list.Clear();
	}

	public static int BKDRHash(string name)
	{
		return Math.Abs ( name.GetHashCode());
	}

	public static void Queue_Lock(LCUI_Queue library)
	{
		
	}

	public static void Queue_Unlock(LCUI_Queue library)
	{

	}

	public static void Queue_Init(LCUI_Queue library,Type type,Object obj)
	{
		if(library.list==null)
		   library.list = new List<object>();
		if (library.widgetList == null)
			library.widgetList = new List<LCUI_Widget_>();
		library.type = type;
	}

	public static void Queue_Init(LCUI_Queue library, Type type, Action<IntPtr> calls)
	{
		if (library.list == null)
			library.list = new List<object>();
		if (library.widgetList == null)
			library.widgetList = new List<LCUI_Widget_>();
		library.type = type;
	}

	public static void Queue_AddPointer(LCUI_Queue library, LCUI_Widget_ widget)
	{
		library.list.Add(widget);
	}

	public static void Queue_UsingPointer(LCUI_Queue library)
	{

	}

	public static void free(object obj)
    {
		obj = null;
	}
}


